<?php
session_start();
define('USERS_FILE', 'users.txt');

// 检查用户是否已登录且需要修改密码
if (!isset($_SESSION['user']) || $_SESSION['need_change_password'] != 1) {
    header('Location: index.php');
    exit;
}

if (isset($_POST['change_password'])) {
    $newPassword = trim($_POST['new_password']);
    $confirmPassword = trim($_POST['confirm_password']);

    if (empty($newPassword) || empty($confirmPassword)) {
        $changeError = "新密码和确认密码不能为空！";
    } elseif ($newPassword != $confirmPassword) {
        $changeError = "新密码和确认密码不一致！";
    } else {
        $hashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);
        $users = file(USERS_FILE, FILE_IGNORE_NEW_LINES);
        $newUsers = [];

        foreach ($users as $user) {
            list($savedUser, $savedPass, $phone, $email, $needChangePassword) = explode('|', $user);
            if ($savedUser === $_SESSION['user']) {
                $newUsers[] = "$savedUser|$hashedPassword|$phone|$email|0";
            } else {
                $newUsers[] = $user;
            }
        }

        if (file_put_contents(USERS_FILE, implode("\n", $newUsers)) !== false) {
            $_SESSION['need_change_password'] = 0;
            header('Location: index.php');
            exit;
        } else {
            $changeError = "修改密码失败，请重试！";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改密码</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
        }

        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        .btn {
            padding: 10px 20px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .btn:hover {
            background-color: #0056b3;
        }

        .error-message {
            color: red;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>修改密码</h2>
        <?php if (isset($changeError)): ?>
            <div class="error-message"><?php echo $changeError; ?></div>
        <?php endif; ?>
        <form method="POST">
            <input type="hidden" name="change_password" value="1">
            <div class="form-group">
                <label for="new_password">新密码</label>
                <input type="password" id="new_password" name="new_password" required>
            </div>
            <div class="form-group">
                <label for="confirm_password">确认密码</label>
                <input type="password" id="confirm_password" name="confirm_password" required>
            </div>
            <button type="submit" class="btn">修改密码</button>
        </form>
    </div>
</body>
</html>